package com.gko100.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Category;
import com.gko100.model.Image;
import com.gko100.model.Language;
import com.gko100.model.Navigation;
import com.gko100.vo.ImageVo;
import com.gko100.vo.NavigationVo;
import com.opensymphony.xwork2.ActionContext;

public class NaviAction extends AdminBaseAction {
	
	private Category cate;
	private List<Category> cateList;
	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}
	public List<Category> getCateList() {
		return cateList;
	}
	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}
	
	private Language lang;

	public Language getLang() {
		return lang;
	}
	public void setLang(Language lang) {
		this.lang = lang;
	}

	private String param;
	private String param_cateId;
	
	public String getParam() {
		return param;
	}
	public void setParam(String param) {
		this.param = param;
	}
	public String getParam_cateId() {
		return param_cateId;
	}
	public void setParam_cateId(String paramCateId) {
		param_cateId = paramCateId;
	}

	private Navigation nav;
	private List<Navigation> navList;
	private Map<Navigation, List<Navigation>> navMap;
	
	public Navigation getNav() {
		return nav;
	}
	public void setNav(Navigation nav) {
		this.nav = nav;
	}
	public List<Navigation> getNavList() {
		return navList;
	}
	public void setNavList(List<Navigation> navList) {
		this.navList = navList;
	}
	
	public Map<Navigation, List<Navigation>> getNavMap() {
		return navMap;
	}
	public void setNavMap(Map<Navigation, List<Navigation>> navMap) {
		this.navMap = navMap;
	}

	private String[] navIdList;
	private String[] navIdList1;
	private String[] navIdList2;
	
	public String[] getNavIdList() {
		return navIdList;
	}
	public void setNavIdList(String[] navIdList) {
		this.navIdList = navIdList;
	}
	public String[] getNavIdList1() {
		return navIdList1;
	}
	public void setNavIdList1(String[] navIdList1) {
		this.navIdList1 = navIdList1;
	}
	public String[] getNavIdList2() {
		return navIdList2;
	}
	public void setNavIdList2(String[] navIdList2) {
		this.navIdList2 = navIdList2;
	}

	
	private NavigationVo nv;
	private List<NavigationVo> nvList;
	private Map<Navigation, List<Navigation>> nvMap;
	
	public NavigationVo getNv() {
		return nv;
	}
	public void setNv(NavigationVo nv) {
		this.nv = nv;
	}
	public List<NavigationVo> getNvList() {
		return nvList;
	}
	public void setNvList(List<NavigationVo> nvList) {
		this.nvList = nvList;
	}
	public Map<Navigation, List<Navigation>> getNvMap() {
		return nvMap;
	}
	public void setNvMap(Map<Navigation, List<Navigation>> nvMap) {
		this.nvMap = nvMap;
	}


	private String checkboxListDel;
	
	public String getCheckboxListDel() {
		return checkboxListDel;
	}
	public void setCheckboxListDel(String checkboxListDel) {
		this.checkboxListDel = checkboxListDel;
	}
	
	
	

	public String find_navList() throws Exception {
		
		
		if(this.getNavList()!=null){
			this.getNavList().clear();
		}
		if(this.getParam_cateId()!=null){
			
			Category cate = adminManager.findCateByCateId(Integer.parseInt(this.getParam_cateId()));
			if(cate!=null){
				this.setCate(cate);
				List<Navigation> nList = new ArrayList<Navigation>();
				List<Category> cList = adminManager.findCateListByCatePid(cate.getCateId());
				if(cList!=null&&cList.size()>=1){
					for(int i=0;i<cList.size();i++){
						
						List<Navigation> nl = adminManager.findNavListByCate(cList.get(i));
						if(nl!=null){
							nList.addAll(nl);
						}
						
					}
				}

				if(nList!=null){
					this.setNavList(nList);
				}
			}

		}
		
		return "navList_find_success";
	}
	
	public String find_navListLev3() throws Exception {
		
		if(this.getNavMap()!=null){
			this.getNavMap().clear();
		}
		
		if(this.getParam()!=null){

			Navigation nav = adminManager.findNavByNavId(Integer.parseInt(this.getParam()));
			if(nav!=null){
				this.setNav(nav);
				List<Navigation> navList = adminManager.findNavListByNavPId(nav.getNavId());
				if(navList!=null&&navList.size()>=1){
					
					this.setNavList(navList);
					Map<Navigation, List<Navigation>> nMap = new LinkedHashMap<Navigation, List<Navigation>>();
					for(int i=0;i<navList.size();i++){
						
						Navigation n = navList.get(i);
						List<Navigation> nList = adminManager.findNavListByNavPId(n.getNavId());
						if(nList!=null){
							nMap.put(n, nList);
						}else{
							nMap.put(n, null);
						}
					}
					this.setNavMap(nMap);
				}
			}
		}

		return "navListLev3_find_success";
	}
	
	
	public String insert_nav() throws Exception{
		
		int navPid = -1;
		if(this.getNav().getNavPid()==0){
			navPid = 0;
		}else if(this.getNav().getNavPid()!=0){
			navPid = this.getNav().getNavPid();
		}
		if(this.getNav()!=null){
			this.setNav(null);
		}
		ActionContext ctx = ActionContext.getContext();
		ctx.getSession().put("navPid", navPid);
		System.out.println("cateId=========="+this.getCate().getCateId());
		if(this.getCate().getCateId()!=null){
			
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			Language lang = cate.getLang();
			System.out.println("lang=========="+lang.getLangName());
			ctx.getSession().put("lang", lang);
			
		}
		if(navPid==0){
			ctx.getSession().put("nav", "nav");	
		}
		
		
		if(navPid!=0){
			Navigation nav = adminManager.findNavByNavId(navPid);
			if(nav!=null){
				this.setNav(nav);
				ctx.getSession().put("nav", "nav");
				ctx.getSession().put("navPid", navPid);
				lang = nav.getLang();
				System.out.println("lang=========="+lang.getLangName());
				ctx.getSession().put("lang", lang);
			}
		}
		
		return "nav_insert_success";
	}
	
	
	public String add_nav() throws Exception {
		
		
		ActionContext ctx = ActionContext.getContext();

		int navPid = 0;
		if(ctx.getSession().get("navPid")!=null){
			navPid = Integer.parseInt(ctx.getSession().get("navPid").toString());
		}
		this.getNav().setNavPid(navPid);
		
		
		if(navPid==0){
			if(this.getNav().getNavNote()!=null){
				System.out.println("navNote=========="+this.getNav().getNavNote());
				Category cate = adminManager.findCateByCateNote(this.getNav().getNavNote());
				if(cate!=null){
					this.getNav().setCate(cate);
				}
				
			}
		}else{
			this.getNav().setCate(null);
		}
		
		
		if(ctx.getSession().get("lang")!=null){
			this.getNav().setLang((Language)(ctx.getSession().get("lang")));
		}
		if(ctx.getSession().get("imageVo")!=null){
			
			ImageVo imageVo = (ImageVo) ctx.getSession().get("imageVo");
			Image image = new Image();
			
			if(imageVo!=null){
				image.setImgSaveName(imageVo.getImageSaveName());
				image.setImgSrcName(imageVo.getImageSrcName());
				image.setImgUrl(imageVo.getImageUrl());
				
				
				adminManager.add_image(image);
				this.getNav().setImage(image);
				ctx.getSession().remove("imageVo");	
			}
		}else{
			this.getNav().setImage(null);
		}
		adminManager.add_nav(this.getNav());
		
		if(ctx.getSession().get("nav")!=null){
			ctx.getSession().remove("nav");
		}
		if(ctx.getSession().get("navPid")!=null){
			ctx.getSession().remove("navPid");
		}
		if(ctx.getSession().get("lang")!=null){
			ctx.getSession().remove("lang");
		}
		if(ctx.getSession().get("cateId")!=null){
			ctx.getSession().remove("cateId");
		}

		return "nav_add_success";
	}
	
	public String del_nav() throws Exception {
		
		if(this.getNav().getNavId()!=null){
			
			Navigation n = adminManager.findNavByNavId(this.getNav().getNavId());
			if(n.getImage()!=null){
				adminManager.del_imageByImage(n.getImage());
			}
			adminManager.del_nav(n);
		}
		
		
		return "nav_del_success";
	}
	
	private Image image;
	
	public Image getImage() {
		return image;
	}
	public void setImage(Image image) {
		this.image = image;
	}
	public String upload_nav() throws Exception {
		
		if(this.getNav().getNavId()!=null){
			ActionContext ctx = ActionContext.getContext();
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){
				this.setNav(nav);
				ctx.getSession().put("nav", nav.getNavNote());
				Category cate = nav.getCate();
				if(cate!=null){
					this.setCate(cate);
				}
				
			}
			
		}

		return "nav_upload_success";
	}
	
	public String modify_nav() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();
		
		if(ctx.getSession().get("imageVo")!=null){
			
			ImageVo imageVo = (ImageVo) ctx.getSession().get("imageVo");
			Image image = new Image();
			
			if(imageVo!=null){
				Image img = this.getNav().getImage();
				adminManager.del_imageByImage(img);
				image.setImgSaveName(imageVo.getImageSaveName());
				image.setImgSrcName(imageVo.getImageSrcName());
				image.setImgUrl(imageVo.getImageUrl());
				adminManager.add_image(image);
				this.getNav().setImage(image);
				ctx.getSession().remove("imageVo");	
			}
		}else{
			this.getNav().setImage(null);
		}
		
		adminManager.modify_navByNav(this.getNav());
		
		return "nav_modify_success";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
